package org.infodavid.common.model.impl;

import org.infodavid.common.model.IEvent;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.model.components.IOwnerDetails;

/**
 * The Class EventImpl.
 */
public class EventImpl extends BasicEventImpl implements IEvent {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -4382933813517861251L;

	/** The administrative flag. */
	private boolean administrative;

	/** The owner details. */
	private IOwnerDetails ownerDetails;

	/**
	 * constructor.
	 * @throws Exception the exception
	 */
	protected EventImpl() throws Exception {
		super();
	}

	/**
	 * constructor.
	 * @param keyArg the key
	 * @throws Exception the exception
	 */
	protected EventImpl(final Long keyArg) throws Exception {
		this();

		setKey(keyArg);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IEvent#getOwnerDetails()
	 */
	public IOwnerDetails getOwnerDetails() {
		return ownerDetails;
	}

	/**
	 * Sets the details.
	 * @param value the new details
	 */
	protected void setOwnerDetails(final IOwnerDetails value) {
		if (!equals(ownerDetails, value)) {
			final Object old = ownerDetails;
			ownerDetails = value;
			markModified(IOwnerDetails.OWNER_DETAILS, old, ownerDetails);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		administrative = false;
		ownerDetails = ModelFactory.getInstance().create(IOwnerDetails.class);
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.bs.model.IEventTemplate#isAdministrative()
	 */
	public boolean isAdministrative() {
		return administrative;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.bs.model.IEventTemplate#setAdministrative(boolean)
	 */
	@SuppressWarnings("boxing")
	public void setAdministrative(final boolean flag) {
		if (!equals(administrative, flag)) {
			administrative = flag;
			markModified(ADMINISTRATIVE);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append("[ownerDetails=");
		builder.append(ownerDetails);
		builder.append(", administrative=");
		builder.append(administrative);
		builder.append("]");

		return builder;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getEndTime() == null ? 0 : getEndTime().hashCode());

		return prime * result + (getStartTime() == null ? 0 : getStartTime().hashCode());
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof IEvent)) {
			return false;
		}

		final IEvent other = (IEvent)obj;

		if (getEndTime() == null) {
			if (other.getEndTime() != null) {
				return false;
			}
		}
		else if (!getEndTime().equals(other.getEndTime())) {
			return false;
		}

		if (getStartTime() == null) {
			if (other.getStartTime() != null) {
				return false;
			}
		}
		else if (!getStartTime().equals(other.getStartTime())) {
			return false;
		}

		return true;
	}
}
